﻿//     Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;

namespace Microsoft.Gateway.Utils
{
    public enum AzureEnvironments
    {
        AzureCloud,
        AzureChinaCloud,
    }

    public class AzureEnvironment
    {
        static readonly IDictionary<AzureEnvironments, EnvironmentInfo> Environments = new Dictionary<AzureEnvironments, EnvironmentInfo>
        {
            { AzureEnvironments.AzureCloud, new EnvironmentInfo 
                {
                    RdfeManagementEndpoint  = "https://management.core.windows.net/",
                    AadLoginEndpoint        = "https://login.windows.net/",
                    StorageAccountUriSuffix = "core.windows.net",
                    CloudServiceUriSuffix   = "cloudapp.net",
                    GraphApiEndpoint        = "https://graph.windows.net/"
                }},
            { AzureEnvironments.AzureChinaCloud, new EnvironmentInfo 
                {
                    RdfeManagementEndpoint  = "https://management.core.chinacloudapi.cn/",
                    AadLoginEndpoint        = "https://login.chinacloudapi.cn/",
                    StorageAccountUriSuffix = "core.chinacloudapi.cn",
                    CloudServiceUriSuffix   = "chinacloudapp.cn",
                    GraphApiEndpoint        = "https://graph.chinacloudapi.cn/"
                }},
        };

        private class EnvironmentInfo
        {
            public string RdfeManagementEndpoint { get; set; }
            public string AadLoginEndpoint { get; set; }
            public string StorageAccountUriSuffix { get; set; }
            public string CloudServiceUriSuffix { get; set; }
            public string GraphApiEndpoint { get; set; }
        }

        static EnvironmentInfo _selectedEnvironment = Environments[AzureEnvironments.AzureCloud];
        public static void InitializeEnvironment(AzureEnvironments environment)
        {
            _selectedEnvironment = Environments[environment];
        }

        public static string RdfeManagementEndpoint
        {
            get { return _selectedEnvironment.RdfeManagementEndpoint; }
        }

        public static Uri RdfeManagementEndpointUri
        {
            get { return new Uri(AzureEnvironment.RdfeManagementEndpoint); }
        }

        public static string AadLoginEndpoint
        {
            get { return _selectedEnvironment.AadLoginEndpoint; }
        }

        public static string StorageAccountEndpoint(string accountName, string storageType)
        {
            return String.Format("https://{0}.{1}.{2}/", accountName, storageType, _selectedEnvironment.StorageAccountUriSuffix);
        }

        public static string AzureCloudServiceUriSuffix
        {
            get { return _selectedEnvironment.CloudServiceUriSuffix; }
        }

        public static string GraphApiEndpoint
        {
            get { return _selectedEnvironment.GraphApiEndpoint; }
        }

        public static string WsFedIssuerCommonEndpoint
        {
            get { return AzureEnvironment.WsFedIssuerEndpoint("common"); }
        }

        public static string WsFedIssuerEndpoint(string tenant)
        {
            return String.Format("{0}{1}/wsfed", _selectedEnvironment.AadLoginEndpoint, tenant);
        }

        public static string FederationMetadataCommonLocation
        {
            get { return AzureEnvironment.FederationMetadataLocation("common"); }
        }

        public static string FederationMetadataLocation(string tenant)
        {
            return String.Format("{0}{1}/FederationMetadata/2007-06/FederationMetadata.xml", _selectedEnvironment.AadLoginEndpoint, tenant);
        }
    }
}
